import {NavTo, Toast} from './uni_api'
import {Public} from '@/Pinia/index'
import type {requestConfig} from '@/TS/utlis/request'

/**
 * @请求封装 requestConfig
 * @config  请求配置信息
 * @request 请求
 */
const BaseUrl : string = 'https://api.waimaishi.cn/app-api'
// const BaseUrl = "http://192.168.1.213:48080/app-api"

// const BaseUrl: string = process.env.NODE_ENV !== 'development' ? 'https://api.waimaishi.cn/app-api' :'http://192.168.1.40:48080/app-api'
let Proxy: any = ''

class requestConfig implements requestConfig.requestConfig {
    config: requestConfig.config

    // 默认配置
    constructor() {
        this.config = {
            url: '',
            data: {},
            params: {},
            header: {},
            method: 'GET',
            timeout: 30000,
            show: true

        }
    }

    // 请求拦截器
    requestconfig(option: requestConfig.option) {
        return new Promise((resolve, reject) => {
            const {Publics} = Public()
            option.url = BaseUrl + option.url
            this.config.header.Authorization = Publics.Authorization
            //获取app版本号信息
            this.config.header.version = uni.getStorageSync('widgetInfo')
            option = {
                ...this.config,
                ...option,
            }
            // console.log("request url = " + option.url)
            // console.log("request params = " + JSON.stringify(option.data))
            option.success = (res: any) => {
                uni.hideLoading()
                resolve(this.reponseconfig(res))
                // console.log("response data = " + JSON.stringify(res.data))
            }

            option.fail = (err: object) => {
                uni.hideLoading()
                Proxy.$API(Proxy).Toast('请求失败，请稍后重试!', 'warning')
                console.log("response err = " + JSON.stringify(err))
            }

            // 先检测网络
            getNetwork().then((res) => {
                // console.log('请求接口')
                uni.request(option)
            })
        })
    }

    // 响应拦截器
    reponseconfig(res: any) {
        return new Promise((resolve, reject) => {
            // 状态码检测
            switch (res.data.code) {
                case 0:
                    // console.log('请求成功')
                    resolve(res.data)
                    break
                case 400:
                    console.error('400错误:' + res.data.msg)
                    reject(res.data)
                    break
                case 401:
                    console.error('401错误:' + res.data.msg)
                    Proxy.$API(Proxy).Toast('登录已过期', 'warning')
                    setTimeout(() => {
                        uni.redirectTo({
                            url: '/pages/Login/Login',
                        })

                    }, 1000)

                    reject(res.data)
                    break
                case 404:
                    console.error('404未找到目标报错:' + res.data.msg)
                    reject(res.data)
                    break
                case 500:
                    console.error('500服务器报错:' + res.data.msg)
                    Proxy.$API(Proxy).Toast('系统错误', 'error')
                    reject(res.data)
                    break
                case -1:
                    console.error('-1:' + res.data.msg)
                    Proxy.$API(Proxy).Toast(res.data.msg, 'error')
                    reject(res.data)
                    break
                default:
                    reject(res.data)
                    console.log(res)
                    console.error(`状态码${res.data.code}: 未知得错误`)
            }
        })
    }

    // 请求
    request(option: requestConfig.option) {
        if (option.show == true) {
            uni.showLoading({
                title: '加载中',
                mask: true,
            })
        }

        return this.requestconfig(option)
    }
}

/**
 * @检查网络状态
 * resolve 有网络
 * reject 无网络
 */
const getNetwork = () => {
    return new Promise((resolve, reject) => {
        uni.getNetworkType({
            success: (res) => {
                if (res.networkType != 'none') {
                    resolve('')
                } else {
                    uni.navigateTo({
                        url: '/pages/Network/Network',
                    })
                }
            },
            fail: (err) => {
                uni.redirectTo({
                    url: '/pages/Network/Network',
                })
                Toast('请查看当前网络连接是否正常')
            },
        })
    })
}

// 请求
const request: requestConfig.requestFn = (option: any, proxy: any) => {
    Proxy = proxy
    return new requestConfig().request(option)
}
export default request
